package com.fs.spring.test.aop.proxy;


import com.fs.spring.test.aop.monitor.PerformanceMonitor;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author shuai.fang
 * @date：2017-08-13
 * @description：
 */
public class CglibProxy implements MethodInterceptor {

	private Enhancer enhancer = new Enhancer();

	public Object getProxy(Class clazz) {
		enhancer.setSuperclass(clazz);
		enhancer.setCallback(this);
		return enhancer.create();
	}


	public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
		PerformanceMonitor.begin(method.getName());
		Object result = methodProxy.invokeSuper(o, objects);
		PerformanceMonitor.end();
		return result;
	}
}
